<html>
<head><meta charset="utf-8"><title>TryFrom&lt;f{N}&gt; for i{N} · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html">TryFrom&lt;f{N}&gt; for i{N}</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238538219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238538219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238538219">(May 12 2021 at 20:22)</a>:</h4>
<p>Hello! This came up as a requested desirable from a user who commonly wants to have a floating-point-to-int conversion that would convert a floating point number to an integer one but would error if it the floating point conversion would come up in a state that normally is handled in an arbitrary fashion by Rust (e.g. by rounding, saturation, NaN to anything else, etc.). This is most commonly for when they think that it <em>should</em> be correctly rounded as an integer already, but they don't want to do the saturation or NaN-fudging that Rust does... those are logic errors for them.</p>
<p>My understanding, if memory serves, is that a stable trait impl on a stable type is instantly stable, so I thought I'd raise a conversation about it first to see what people might have a taste for. It also seems particularly relevant with the edition bringing TryFrom/TryInto into scope in the prelude.</p>



<a name="238540445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238540445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238540445">(May 12 2021 at 20:37)</a>:</h4>
<p>sounds like a good idea to me!</p>



<a name="238540691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238540691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238540691">(May 12 2021 at 20:38)</a>:</h4>
<p>I'd also add <code>TryFrom&lt;f{N}&gt; for u{M}</code> where <code>N</code> doesn't have to match <code>M</code></p>



<a name="238545935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238545935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238545935">(May 12 2021 at 21:14)</a>:</h4>
<p>See <a href="https://github.com/rust-lang/rfcs/pull/2484">Conversions: FromLossy and TryFromLossy traits #2484</a> for a bunch of conversations related to this.</p>



<a name="238546371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238546371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238546371">(May 12 2021 at 21:18)</a>:</h4>
<p><span class="user-mention" data-user-id="281757">@Jubilee</span> Any idea how those "supposed to be exactly an integer" values are produced?  The usual <code>0.1 * 10.0 != 1.0</code> kind of problems make me unsure how useful the cast would be, vs <code>round_to_int</code> or <code>floor_to_int</code> or something instead of <code>.floor().try_into().unwrap()</code>...</p>



<a name="238548430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238548430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238548430">(May 12 2021 at 21:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D/near/238546371">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="281757">Jubilee</span> Any idea how those "supposed to be exactly an integer" values are produced?  The usual <code>0.1 * 10.0 != 1.0</code> kind of problems make me unsure how useful the cast would be, vs <code>round_to_int</code> or <code>floor_to_int</code> or something instead of <code>.floor().try_into().unwrap()</code>...</p>
</blockquote>
<p><code>0.1 * 10.0 == 1.0</code> is true for both binary32 and binary64 values, actually.</p>
<p>The only case where this is false is if on a platform that only uses long doubles, which could twist the result in a way the user doesn't want, so the user might indeed want to use this fallible conversion to become more aware of such errors on specific platforms.</p>



<a name="238548853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238548853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238548853">(May 12 2021 at 21:40)</a>:</h4>
<p>So yes, <code>floor().try_into().unwrap()</code> is more verbose, and I agree it might in principle be sensible to offer such a function, but some of us are in fact <em>absolute demon spawn</em> and know how to do exact floating point computations and want to manage the results for whatever demented reason. :^)</p>



<a name="238552108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238552108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238552108">(May 12 2021 at 22:10)</a>:</h4>
<p>yay for bit manipulation by using <code>f32</code>!</p>



<a name="238552373"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238552373" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238552373">(May 12 2021 at 22:13)</a>:</h4>
<p>I guess I should have checked my example.  <code>235.0 * (1/235.0)</code>, then, which came up on Discord the other day so I know for certain it's not <code>1.0</code>.</p>



<a name="238552878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238552878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238552878">(May 12 2021 at 22:18)</a>:</h4>
<p>I agree that it's <em>possible</em> to write code that produces exact integer answers.  I'm just not convinced that it's common enough that <code>try_into()</code> is right way to represent it.  For example, the compiler often suggests that for type mismatches (when just <code>into</code> isn't available), and I think that such a suggestion would most often be misguided for people who have a float and want to have an integer.</p>



<a name="238553571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238553571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238553571">(May 12 2021 at 22:26)</a>:</h4>
<p><code>TryInto</code> would not be a good fit there because there are multiple valid interpretations of what that would "mean" (Does it just check for in-bounds? Exact integer? etc)</p>



<a name="238553611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238553611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238553611">(May 12 2021 at 22:26)</a>:</h4>
<p>Java has a <code>toIntExact</code> method for that kind of cast and something similar on floats seems pretty reasonable though</p>



<a name="238553983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238553983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238553983">(May 12 2021 at 22:30)</a>:</h4>
<p>the float to int casting in <code>num-traits</code> just checks for in-bounds -- <code>as</code> without saturation or NaN</p>



<a name="238561624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238561624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238561624">(May 12 2021 at 23:59)</a>:</h4>
<p>There's already a never-stable <a href="https://doc.rust-lang.org/std/convert/trait.FloatToInt.html">https://doc.rust-lang.org/std/convert/trait.FloatToInt.html</a> for <code>to_int_unchecked</code>; would it make sense to use that to add a <code>to_int_exact</code> that works with all the integer types?</p>



<a name="238674642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238674642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238674642">(May 13 2021 at 20:16)</a>:</h4>
<p>Hmm, I think "there's multiple valid interpretations" is both a reasonable objection but at least at first it does not seem to be a terribly strong one? It seems to me there's always multiple valid interpretations of any conversion, so canonizing one with an arbitrary decision as the trait impl seems no more odd than the canonization of certain things for <code>as</code> casts. I am certainly open to considering the <code>to_int_exact</code> function path, but I would like to better understand the reason to reject arbitrariness here.</p>
<p>Though I should also admit, to me, saying <code>TryInto</code> turns the float into an integer if it can be exactly converted <strong>is</strong> just a "bounds check"... there just happen to be 2^N values it is checking it is in-bounds-of, and floating point, by having a somewhat more... interesting data formation that allows for compression,  can be a value between each of those 2^N bounds, or beyond. So I guess my sentiment is something like "TryInto should <em>obviously</em> be a non-coercive cast? Why would it be a coercive one?"</p>



<a name="238678901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238678901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238678901">(May 13 2021 at 20:49)</a>:</h4>
<p>...Also I don't want to be mean by picking on your examples but <code>235.0 * (1.0/235.0) == 1.0</code> is actually also true for f64! ...but not f32. See, this is kind of the "point" of such a method, to me. ^^;</p>



<a name="238679388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238679388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238679388">(May 13 2021 at 20:52)</a>:</h4>
<p>I think there's a lot of benefits to <em>having</em> a method for this, regardless of whether there's also the trait impl (and I'm not sure I'd want to see code using the TryFrom trait generically for this purpose)- since we can add the method as unstable, this could start there?</p>



<a name="238680628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238680628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238680628">(May 13 2021 at 21:01)</a>:</h4>
<p>Alright. Sounds good for now.<br>
I am at least persuaded that I should probably write an RFC or something if I want to seriously propose a trait stabilization here.</p>



<a name="238688775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/TryFrom%3Cf%7BN%7D%3E%20for%20i%7BN%7D/near/238688775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/TryFrom.3Cf.7BN.7D.3E.20for.20i.7BN.7D.html#238688775">(May 13 2021 at 22:10)</a>:</h4>
<p>Oh, I was asked about the reverse: integers to floats using try_{from,into}. Would that be considered a simpler case?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>